package com.luwak.controller.face;

import com.luwak.common.base.R;
import com.luwak.common.utils.FaceMessageUtils;
import com.luwak.common.utils.IDCardMessageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * @author ChenXc
 * @version V1.0
 * @Date 2018/4/7 下午1:27
 * @Description TODO(面部识别)
 */
@Api(value = "面部识别", description = "识别面部属性", tags = {"识别面部属性"})
@RestController
@RequestMapping("/face")
public class FaceController {

    @PostMapping("/getAttributes")
    @ApiOperation("面部识别")
    public R getFaceAttributes(@ApiParam(value = "上传图片", required = true) MultipartFile multipartFile) {

        String result = null;
        File file = null;

        if (!multipartFile.isEmpty()) {
            try {
                file = File.createTempFile("tmp", null);
                multipartFile.transferTo(file);
                file.deleteOnExit();
                result = FaceMessageUtils.getFaceInfoByFile(file);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return R.ok().put("data", result);
    }

    @PostMapping("getIDCardAttributes")
    @ApiOperation("身份证识别")
    public R getIDCardAttributes(@ApiParam(value = "上传图片", required = true) MultipartFile multipartFile) {

        String result = null;
        File file = null;
        if (!multipartFile.isEmpty()) {
            try {
                file = File.createTempFile("tmp", null);
                multipartFile.transferTo(file);
                file.deleteOnExit();
                result = IDCardMessageUtils.getIDCardInfo(file);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return R.ok().put("data", result);
    }
}
